package com.yu8home.opcua.test;

import com.yu8home.opcua.OpcUAUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedEventItem;
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.structured.EventFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class EventChangeT {

    public static void main(String[] args) throws Exception {
        OpcUAUtil.getConnect();
        createSubscribe();
        System.in.read();
    }

    public static void createSubscribe() throws Exception {
        List<NodeId> nodeIds = new ArrayList<>();
        List<EventFilter> eventFilters = new ArrayList<>();

        for (String s : Const.subscribes) {
            nodeIds.add(new NodeId(OpcUAUtil.namespaceIndex, s));
            eventFilters.add(Const.eventFilter);
        }

        ManagedSubscription subscription = ManagedSubscription.create(OpcUAUtil.opcUaClient);


//        add("k");
//        add("k.Simulation Examples_Functions_k");
//        add("k.Simulation Examples_Functions_k.MULTI_LEVEL");
//
//        add("_CustomAlarms.k");
//        add("_CustomAlarms.k.Simulation Examples_Functions_k");
//        add("_CustomAlarms.k.Simulation Examples_Functions_k.MULTI_LEVEL");


        NodeId nodeId1 = new NodeId(OpcUAUtil.namespaceIndex, "_CustomAlarms.k");
        NodeId nodeId2 = new NodeId(OpcUAUtil.namespaceIndex, "_CustomAlarms.k.Simulation Examples_Functions_k");
        NodeId nodeId3 = new NodeId(OpcUAUtil.namespaceIndex, "_CustomAlarms.k.Simulation Examples_Functions_k.MULTI_LEVEL");

        ManagedEventItem eventItem1 = subscription.createEventItem(nodeId1, Const.eventFilter);
        ManagedEventItem eventItem2 = subscription.createEventItem(nodeId2, Const.eventFilter);
        ManagedEventItem eventItem3 = subscription.createEventItem(nodeId3, Const.eventFilter);
        // ManagedEventItem eventItem4 = subscription.createEventItem(Identifiers.Server, Const.eventFilter);

        final CountDownLatch eventLatch = new CountDownLatch(3);
        Thread.sleep(10 * 1000);

        int size = subscription.getEventItems().size();
        log.info("eventItems.size：{}", size);

        subscription.addEventChangeListener((eventItems, variants) -> {
            for (int i = 0; i < eventItems.size(); i++) {
                ManagedEventItem eventItem = eventItems.get(i);
                Variant[] eventFieldValues = variants.get(i);

                log.info("标识符：{}", eventItem.getNodeId().getIdentifier().toString());

                for (int j = 0; j < eventFieldValues.length; j++) {
                    log.info("变量variant[{}]: {}", j, eventFieldValues[j].getValue());
                }
            }
            eventLatch.countDown();
        });

        eventLatch.await();
        eventItem1.delete();
        eventItem2.delete();
        eventItem3.delete();
        // eventItem4.delete();
    }

}

